package com.hu.web.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class SecondServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("SecondServlet 的 doGet 方法执行");

        ServletContext servletContext = req.getServletContext();
        System.out.println("servletContext: " + servletContext);

        // 获取 ServletContext 作用域对象中设置的共享数据
        Object msg = servletContext.getAttribute("msg");
        System.out.println("msg: " + msg);

        // 移除 ServletContext 作用域对象中指定的数据
        servletContext.removeAttribute("msg");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("SecondServlet 的 doPost 方法执行");
    }

}
